﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;
using Monkey.Warrenty.Enum;
using Resources;

namespace MonkeyWarranty.Web.EmployeePages.InstantTickets
{
    public partial class EditProduct : System.Web.UI.Page
    {
        #region Properties

        private InstantTicket_Product _ticketProduct;
        private InstantTicket_Product TicketProduct
        {
            get
            {
                if (_ticketProduct == null)
                {
                    if (TicketProductId > 0)
                    {
                        _ticketProduct = InstantTicketManager.GetTicketProductById(TicketProductId);
                    }
                }
                return _ticketProduct;
            }
        }

        private static CustomerBiz _customerBiz;
        private static CustomerBiz CustomerManager
        {
            get { return _customerBiz ?? (_customerBiz = new CustomerBiz()); }
        }

        private long _ticketProductId;
        private long TicketProductId
        {
            get
            {
                if (_ticketProductId <= 0) long.TryParse(Request.Params["id"], out _ticketProductId);
                return _ticketProductId;
            }
        }

        protected long TicketId
        {
            get { return (TicketProduct != null) ? TicketProduct.InstantTicketId : 0; }
        }

        private static InstantTicketBiz _instantTicketBiz;
        private static InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        private InstantTicketProductView _productView;
        protected InstantTicketProductView CurrentProductView
        {
            get { return _productView ?? (_productView = ProductManager.GetProductViewById(TicketProductId)); }
        }

        private static ProductBiz _productBiz;
        private static ProductBiz ProductManager
        {
            get { return _productBiz ?? (_productBiz = new ProductBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (TicketProductId <= 0)
                {
                    errorMessage.Visible = true;
                    lblErrorMessage.Text = GlobalStrings.ProductNotExisted;
                    btnProcessing.Enabled = false;
                    btnComplete.Enabled = false;
                    return;
                }

                LoadData();
            }
        }

        protected void btnProcessing_Click(object sender, EventArgs e)
        {
            errorMessage.Visible = false;
            lblErrorMessage.Text = string.Empty;

            // Call Biz Save Product
            InstantTicketManager.UpdateTicketProductStatus(TicketProductId, (int)InstantProductStatus.Processing, txtDescription.Text, txtNote.Text);

            errorMessage.Visible = true;
            lblErrorMessage.Text = GlobalStrings.UpdateSuccess;

            btnProcessing.Visible = false;
        }

        protected void btnComplete_Click(object sender, EventArgs e)
        {
            errorMessage.Visible = false;
            lblErrorMessage.Text = string.Empty;

            // Call Biz Save Product
            InstantTicketManager.UpdateTicketProductStatus(TicketProductId, (int)InstantProductStatus.Completed, txtDescription.Text, txtNote.Text);

            errorMessage.Visible = true;
            lblErrorMessage.Text = GlobalStrings.UpdateSuccess;

            btnProcessing.Visible = false;
        }

        #endregion

        #region Helpers

        private void LoadData()
        {
            // instant ticket
            var ticket = InstantTicketManager.GetById(TicketId);
            if (ticket == null)
            {
                return;
            }
            ltrTicketCode.Text = ticket.Code;
            ltrContent.Text = ticket.TicketContent;
            ltrDistance.Text = string.Format("{0:0.00}", ticket.Distance);
            ltrReceivedDate.Text = string.Format("{0:dd/MM/yyyy HH:mm}", ticket.CreatedDate);
            ltrDescription.Text = string.IsNullOrEmpty(ticket.Description) ? string.Empty : ticket.Description.Replace("\n", "<br />");

            // customer
            var customer = CustomerManager.GetById(ticket.CustomerId);
            if (customer != null)
            {
                ltrCustomerName.Text = customer.Name;
                ltrCustomerAddress.Text = customer.Address;
                ltrCustomerPhone.Text = customer.Phone;
                ltrCustomerEmail.Text = customer.Email;
            }

            // product
            if (CurrentProductView == null)
            {
                return;
            }

            ltrName.Text = CurrentProductView.ProductName;
            ltrModel.Text = CurrentProductView.Model;
            ltrSerial.Text = CurrentProductView.Serial;
            ltrQuantity.Text = CurrentProductView.Quantity.ToString(CultureInfo.InvariantCulture);
            txtDescription.Text = CurrentProductView.Description;
            ltrWarrantyDate.Text = string.Format("{0:dd/MM/yyyy HH:mm}", CurrentProductView.WarrantyDate);
            ltrMonths.Text = string.Format("{0:0.0}", CurrentProductView.Months);
            ltrWarrantyCount.Text = string.Format("{0}", CurrentProductView.WarrantyCount);
            txtNote.Text = CurrentProductView.Note;

            ltrStartDate.Text = CurrentProductView.StartDateString;
            ltrEndDate.Text = CurrentProductView.EndDateString;
            ltrManufacturer1.Text = CurrentProductView.ManufacturerName;
            ltrWarrantyType.Text = CurrentProductView.WarrantyTypeName;
            ltrError.Text = CurrentProductView.ErrorName;
            ltrSupplier.Text = CurrentProductView.SupplierName;
            ltrEmployee.Text = CurrentProductView.EmployeeName;
            ltrStatus.Text = CurrentProductView.InstantProductStatusName;

            if ((InstantProductStatus)CurrentProductView.InstantProductStateId != InstantProductStatus.Received)
            {
                btnProcessing.Visible = false;
            }
        }

        #endregion
    }
}